package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.GsonBuilder;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.product.CatalogPropertySaveModel;
import com.ymatou.iapi.productmanager.parameter.product.CatalogSaveModel;
import com.ymatou.iapi.productmanager.parameter.product.ModifySellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.product.ProductDescSaveModel;
import com.ymatou.iapi.productmanager.parameter.product.ProductFreightSaveModel;
import com.ymatou.iapi.productmanager.parameter.product.ProductPropertySaveModel;
import com.ymatou.iapi.productmanager.parameter.product.ProductSaveModel;
import com.ymatou.iapi.productmanager.service.AddSellerProductCall;
import com.ymatou.iapi.productmanager.service.GetProductAndCatalogInfoCall;
import com.ymatou.iapi.productmanager.parameter.product.ProductVideoSaveModel;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;

public class ModifyProductCallService {
	public static ModifySellerProductRequest getModifySellerProductRequestFromFile(String fileName,Map<String, Object> updateMap) {
		if (fileName == null || fileName.equals("")) {
			fileName = "modifysellerproductcalltpl.json";
		}

		String fullPath = "com.ymatou.iapi.productmanager/ModifySellerProductCall/" + fileName + ".json";

		JSONObject jsonBean = JsonBeanHelper.getJsonBean(fullPath);

		JsonBeanHelper.updateJsonBean(jsonBean, updateMap);

		String jsonStr = jsonBean.toString();

		return new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(jsonStr,
				ModifySellerProductRequest.class);

	}
	
	
	
	public static ModifySellerProductRequest getModifySellerProductRequestFromAddProductBean(String productId,AddSellerProductRequest addBean){
		IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
		
		ModifySellerProductRequest modifyBean = new ModifySellerProductRequest();
		modifyBean.setFirstRelease(addBean.isFirstRelease());
		modifyBean.setProduct(addBean.getProduct());
		modifyBean.setSellerId(addBean.getSellerId());
		modifyBean.setProductId(productId);
		modifyBean.setProductSite(addBean.getProductSite());
		modifyBean.setSellerName(addBean.getSellerName());
		modifyBean.getProduct().setProductId(productId);
		modifyBean.getProduct().setReleaseProduct(false);
		
		List<CatalogSaveModel> catalogListReq = modifyBean.getProduct().getCatalogList();
		List<CatalogSaveModel> catalogSaveModelInSql = new ArrayList<>();
		
		
		List<Map<String,Object>> catalogsInSqlMap = ipm.selectCatalogsByProdId(productId);
		for(Map<String,Object> catalogMap : catalogsInSqlMap){
			CatalogSaveModel catalogSaveModel = new CatalogSaveModel();
			catalogSaveModel.setCatalogId(catalogMap.get("sCatalogId").toString());			
			
			//属性
			List<CatalogPropertySaveModel> propertyList = new ArrayList<>();
			List<Map<String,Object>> proAndValMapList = ipm.selectCatalogProAndValByCatalogId(catalogMap.get("sCatalogId").toString());
			
			for(Map<String,Object> proAndValMap: proAndValMapList){
				CatalogPropertySaveModel catalogPropertySaveModel = new CatalogPropertySaveModel();
				catalogPropertySaveModel.setPropertyName(proAndValMap.get("sProductProperty").toString());
				catalogPropertySaveModel.setPropertyValue(proAndValMap.get("sCategoryAttribute").toString());
				catalogPropertySaveModel.setPropertyUrl((proAndValMap.get("sPicUrl") == null) ? null : proAndValMap.get("sPicUrl").toString());
				propertyList.add(catalogPropertySaveModel);
			}
			
			catalogSaveModel.setPropertyList(propertyList);
			catalogSaveModelInSql.add(catalogSaveModel);
		}
		
		
		//找到对应匹配请求的规格 设置catalog值 
		List<String> catalogIds = catalogsInSqlMap.stream().map(x->x.get("sCatalogId").toString()).collect(Collectors.toList());
		
		if (catalogListReq.size()==1){
			//单规格
			catalogListReq.get(0).setCatalogId(catalogIds.get(0));
		}else{
			//多规格
			for (CatalogSaveModel catalogSaveModelReq:catalogListReq){
				if (catalogSaveModelReq.getPropertyList().size()==1){
					//单属性
					String property  = catalogSaveModelReq.getPropertyList().get(0).getPropertyValue();
					CatalogSaveModel sqlCatalog = catalogSaveModelInSql.stream()
															.filter(x->x.getPropertyList().get(0).getPropertyValue().equals(property))
															.findFirst().orElse(null);
//					Logger.verifyIsNull(sqlCatalog, "该属性应该存在规格:["+property+"]");
					catalogSaveModelReq.setCatalogId(sqlCatalog.getCatalogId());
					
				}else{
					//双属性
					String property1 = catalogSaveModelReq.getPropertyList().get(0).getPropertyValue();
					String property2 = catalogSaveModelReq.getPropertyList().get(1).getPropertyValue();
					CatalogSaveModel sqlCatalog = catalogSaveModelInSql.stream()
							.filter(x->(x.getPropertyList().get(0).getPropertyValue().equals(property1) && x.getPropertyList().get(1).getPropertyValue().equals(property2))
										||(x.getPropertyList().get(1).getPropertyValue().equals(property1) && x.getPropertyList().get(0).getPropertyValue().equals(property2)))
										.findFirst().orElse(null);
					
//					Logger.verifyNotNull(sqlCatalog, "该属性应该存在规格:["+property1+","+property2+"]");		
					catalogSaveModelReq.setCatalogId(sqlCatalog.getCatalogId());
					catalogSaveModelReq.setOperateType(2);
				}
			}
		}
		
		return modifyBean;
	}
	
	
	public static ModifySellerProductRequest getModifySellerProductRequestFromAddProductBean(AddSellerProductRequest addBean) throws JSONException, ClientProtocolException, IOException{
		//JSONObject ret = new AddSellerProductCall().callAndGetReturnData(addBean);
		AddSellerProductCall addSellerProductCall=new AddSellerProductCall();
		addSellerProductCall.setData(addBean);
		
	       //物流权限1
        addSellerProductCall.addHeader("mockId-1", "GUID");
        addSellerProductCall.addHeader("mockApi-1", "orderdelivery-GetAvailableDeliveryMethod-Normal");
        //自动巡检
        addSellerProductCall.addHeader("mockId-2", "GUID");
        addSellerProductCall.addHeader("mockApi-2", "Violate-autoPreViolateProduct-Normal");
        //买手专项权限
        addSellerProductCall.addHeader("mockId-3", "GUID");
        addSellerProductCall.addHeader("mockApi-3", "sellerallocation-checkSellerSpecialPermission-Normal");
        //物流权限2
        addSellerProductCall.addHeader("mockId-4", "GUID");
        addSellerProductCall.addHeader("mockApi-4", "orderdelivery-GetAvailableDeliveryMethodV2-Normal");
        //发商品的数量
		addSellerProductCall.addHeader("mockId-5", "GUID");
		addSellerProductCall.addHeader("mockApi-5", "SellerService-getSellerSeat-Normal");
		
		addSellerProductCall.callService();

		
		JSONObject ret = new JSONObject(addSellerProductCall.getReturnData());
		String productId = ret.getJSONObject("Data").getString("ProductId");
		return getModifySellerProductRequestFromAddProductBean(productId,addBean);
	}
	
	
	
	public static void checkAddProduct(ModifySellerProductRequest modifySellerProductRequest) throws Exception{
		String productId = modifySellerProductRequest.getProductId();
		
		ModifySellerProductRequest actInSql = new ModifySellerProductRequest();
		ProductSaveModel productSaveModel = new ProductSaveModel();
		
		IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
		
		//品牌名从库里拿
		String brandEn = ipm.selectBrandByBrandId(modifySellerProductRequest.getProduct().getBrandId()).get("sBrandEn").toString();
		modifySellerProductRequest.getProduct().setBrandName(brandEn);
	
		// SQL Products 
		Map<String,Object> productInMap = ipm.selectProductByProdId(productId);
		Logger.verifyEquals(modifySellerProductRequest.getProductId(), productInMap.get("sProductId").toString(), "验证ProductId");
		Logger.verifyEquals(modifySellerProductRequest.getSellerId(), productInMap.get("iUserId"), "验证SellerId");
		Logger.verifyEquals(modifySellerProductRequest.getSellerName(), productInMap.get("SellerLoginId").toString(), "验证SellerName");
//		Logger.verifyEquals(modifySellerProductRequest.getProductSite(), productInMap.get("sSiteId").toString(), "验证ProductSite");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getRemark(), productInMap.get("Remark").toString(), "验证Remark");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getSpuId(), productInMap.get("iSpuId"), "验证SpuId");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getProductName(), productInMap.get("sProduct").toString(), "验证ProductName");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getThirdCategoryId(), (Integer)productInMap.get("iThirdCategoryId"), "验证ThirdCategoryId");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getSecondCategoryId().toString(),  productInMap.get("iCategoryId").toString(), "验证CategoryId");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getBrandId().toString(), productInMap.get("iBrandId").toString(), "验证BrandId");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getBrandName(), productInMap.get("sBrand").toString(), "验证BrandName");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getCatalogStatus().toString(), productInMap.get("iCatalogStatus").toString(), "验证CatalogStatus");
		
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getCatalogType(), (Integer)productInMap.get("iCatalogType"),"验证CatalogType");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getTariffType(), (Integer)productInMap.get("iTariffType"),"验证TariffType");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getBondedArea(), (Integer)productInMap.get("iBondedArea"),"验证BondedArea");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getProductCode(), (productInMap.get("sProductCode") == null) ? null : productInMap.get("sProductCode").toString(),"验证ProductCode");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getMultiLogistics(), (Integer)productInMap.get("iMultiLogistics"),"验证MultiLogistics");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getFlightBalance(), new BigDecimal(productInMap.get("dFlightBalance").toString()).doubleValue(),"验证FlightBalance");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getWeightUnit(), (Integer)productInMap.get("WeightUnit"),"验证WeightUnit");
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getSellerCountryId(), (Integer)productInMap.get("SellerCountryId"),"验证SellerCountryId");
		
		actInSql.setProduct(productSaveModel);
		//图片 商品表保存首图
		Logger.verifyEquals(modifySellerProductRequest.getProduct().getProductPicList().get(0),
			productInMap.get("sPicUrl").toString(), "YmtProduct商品表保存首图");
		
		//validstart validend
		Date reqStartDate = YMTDateUtil.getDate(modifySellerProductRequest.getProduct().getValidStart(),YMTDateUtil.YMDHMS);
		Date reqEndDate = YMTDateUtil.getDate(modifySellerProductRequest.getProduct().getValidEnd(),YMTDateUtil.YMDHMS);
		Date startDateInSql = (Date)productInMap.get("validStart");
		Date endDateInSql = (Date)productInMap.get("validEnd");
		
		AssertService.assertTime(reqStartDate, startDateInSql, 120,"validstart");
		AssertService.assertTime(reqEndDate, endDateInSql,120, "validend");
		
		//ProductPicture
		List<String> productPics = ipm.selectPictureByProdIdAndAction0(productId).stream()
										.map(x->x.get("sOriUrl").toString())
										.collect(Collectors.toList());
		productSaveModel.setProductPicList(productPics);
		
		
		//文描节点
		setProductDescSaveModel(modifySellerProductRequest,productSaveModel);

		//视频节点
		setproductVideoSaveModel(modifySellerProductRequest,productSaveModel);
		
		//属性
		setProductPropertySaveModel(modifySellerProductRequest,productSaveModel);
		
		//运费模板
		setProductFreightSaveModel(modifySellerProductRequest,productSaveModel);
		
		//todo
		//规格
		//如有新增规格 通过价格值来区分定位
		//删除规格 不在最终对象比对中 直接判断action为-1即可
		setCatalogSaveModel(modifySellerProductRequest,productSaveModel);
		
		//直播检查
		checkProductLive(modifySellerProductRequest.getSellerId(),modifySellerProductRequest.getProductId(),modifySellerProductRequest.getProduct().getLiveIdList());
		
		//商品等级检查
		String prodGrade = getProductGrade(modifySellerProductRequest.getProduct());
		Logger.verifyEquals(prodGrade,productInMap.get("sGrade").toString(),"Grade检查");
//		Logger.debug("modifySellerProductRequest:"+FormatUtil.beanToGSONString(modifySellerProductRequest));
//		Logger.debug("actInSql:"+FormatUtil.beanToGSONString(actInSql));
	}
	
	//规格检查
	private static void setCatalogSaveModel(ModifySellerProductRequest modifySellerProductRequest,ProductSaveModel productSaveModel){
		String productId = modifySellerProductRequest.getProductId();
		
		List<CatalogSaveModel> catalogList = new ArrayList<>();
		//catalogs
		IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
		
		List<Map<String,Object>> catalogsInSqlMap = ipm.selectCatalogsByProdId(productId);
		 
		int i=-1;
		for(Map<String,Object> catalogMap : catalogsInSqlMap){
			i++;
			if ((int)catalogMap.get("iAction")==-1){
				String delCatalogId = catalogMap.get("sCatalogId").toString();
				CatalogSaveModel catalogSaveModel = modifySellerProductRequest.getProduct().getCatalogList()
											.stream().filter(x->String.valueOf(x.getCatalogId()).equals(delCatalogId)).findFirst().orElse(null);
				if(catalogSaveModel!=null && catalogSaveModel.getCatalogId()!="" && catalogSaveModel.getOperateType()!=3)
				{
					Logger.verifyEquals(3, catalogSaveModel.getOperateType(), "删除商品 OperateType应为3");
					modifySellerProductRequest.getProduct().getCatalogList().remove(catalogSaveModel);
				}
				else if (catalogSaveModel!=null && ((catalogSaveModel.getCatalogId()=="") || 
						(catalogSaveModel.getCatalogId()!="" && catalogSaveModel.getOperateType()==3))){
					modifySellerProductRequest.getProduct().getCatalogList().remove(catalogSaveModel);
				}
//				if(catalogSaveModel==null )
//				{
//					List<Map<String,Object>> cataList=ipm.selectCatalogProAndValByCatalogId(delCatalogId);
//					if(modifySellerProductRequest.getProduct().getCatalogList().contains(cataList))
//						 modifySellerProductRequest.getProduct().getCatalogList().remove(i);
//				}
				//modifySellerProductRequest 移除、CatalogId=""、OperateType=3 都表示删除规格
				/*else if(catalogSaveModel==null && modifySellerProductRequest.getProduct().getCatalogList().get(i)!=null && modifySellerProductRequest.getProduct().getCatalogList().get(i).getCatalogId()=="")
				{
					List<Map<String,Object>> cataList=ipm.selectCatalogProAndValByCatalogId(delCatalogId);
					if(modifySellerProductRequest.getProduct().getCatalogList().contains(cataList))
						
					   modifySellerProductRequest.getProduct().getCatalogList().remove(i);
				}*/
				
			}else{
				CatalogSaveModel catalogSaveModel = new CatalogSaveModel();
				catalogSaveModel.setCatalogId(catalogMap.get("sCatalogId").toString());			
				catalogSaveModel.setPackage((Boolean)catalogMap.get("IsPackage"));
				catalogSaveModel.setMultiLogistics((Integer)catalogMap.get("iMultiLogistics"));
				catalogSaveModel.setPrice(new BigDecimal(catalogMap.get("fQuotePrice").toString()).doubleValue());
				catalogSaveModel.setVipPrice(new BigDecimal(catalogMap.get("fVipPrice").toString()).doubleValue());
				catalogSaveModel.setNewPrice(new BigDecimal(catalogMap.get("fNewCustomerPrice").toString()).doubleValue());
				catalogSaveModel.setStock((Integer)catalogMap.get("iNum"));
				catalogSaveModel.setSku((catalogMap.get("sSKU") == null) ? null : catalogMap.get("sSKU").toString());
				catalogSaveModel.setProductCode((catalogMap.get("sProductCode") == null) ? null : catalogMap.get("sProductCode").toString());
				catalogSaveModel.setPreSale((Boolean)catalogMap.get("IsPreSale"));
				catalogSaveModel.setWeight(new BigDecimal(catalogMap.get("Weight").toString()).doubleValue());
				catalogSaveModel.setOperateType(1);
				catalogSaveModel.setAction((Integer)catalogMap.get("iAction"));
				
				
				//获取该规格有效
				List<Integer> subCatalogList = ipm.selectSubCatalogByCatalogId(catalogMap.get("sCatalogId").toString())
									.stream().filter(x->(Integer)x.get("Action")>-1)
									.map(y->(Integer)y.get("SubCatalogId"))
									.collect(Collectors.toList());
				
				catalogSaveModel.setSubCatalogIdList(subCatalogList.size()==0?null:subCatalogList);
				
				
				
				
				
				//属性
				List<CatalogPropertySaveModel> propertyList = new ArrayList<>();
				List<Map<String,Object>> proAndValMapList = ipm.selectCatalogProAndValByCatalogId(catalogMap.get("sCatalogId").toString());
				
				for(Map<String,Object> proAndValMap: proAndValMapList){
					CatalogPropertySaveModel catalogPropertySaveModel = new CatalogPropertySaveModel();
					catalogPropertySaveModel.setPropertyName(proAndValMap.get("sProductProperty").toString());
					catalogPropertySaveModel.setPropertyValue(proAndValMap.get("sCategoryAttribute").toString());
					catalogPropertySaveModel.setPropertyUrl((proAndValMap.get("sPicUrl") == null) ? null : proAndValMap.get("sPicUrl").toString());
					propertyList.add(catalogPropertySaveModel);
				}
				
				catalogSaveModel.setPropertyList(propertyList);
				
				
				
				//新增规格id 设置
				CatalogSaveModel catalogModelInReq = modifySellerProductRequest.getProduct().getCatalogList().stream()
						.filter(x->(x.getCatalogId()==null||x.getCatalogId().equals(""))&&(x.getOperateType()==0||x.getOperateType()==1)&&x.getPrice()==catalogSaveModel.getPrice())
						.findFirst().orElse(null);
				if (catalogModelInReq!=null){
					catalogModelInReq.setCatalogId(catalogSaveModel.getCatalogId());
				}
				catalogList.add(catalogSaveModel);
			}
		}
		productSaveModel.setCatalogList(catalogList);
	}
		
		
		
	//check 直播商品
	@SuppressWarnings("unchecked")
	private static void checkProductLive(int sellerId,String productId, List<Integer> liveIds) throws Exception {
		if (liveIds == null || liveIds.size() == 0) {
			return;
		}

		for (int liveId : liveIds) {
			Map<String, Object> liveMapUpdate = new YmtLiveWapper().selectforMapByLiveId(liveId).get(0);
			boolean isLiveStart = false;
			if (((Date) liveMapUpdate.get("dBeginTime")).before(YMTDateUtil.getDate())
					&& ((Date) liveMapUpdate.get("dEndTime")).after(YMTDateUtil.getDate())) {
				isLiveStart = true;
			}

			Map<String, Object> liveProductMap = new YmtProductsInLiveWapper().selectInLiveByProdIdAndLiveId(productId,liveId).get(0);
					

			Logger.verifyEquals(sellerId, Integer.parseInt(liveProductMap.get("iUserId").toString()),
					"ProductInLive - iUserId");
			Logger.verifyEquals(liveId, Integer.parseInt(liveProductMap.get("iActivityId").toString()),
					"ProductInLive - iActivityId");
			Logger.verifyEquals(0, Integer.parseInt(liveProductMap.get("iAction").toString()),
					"ProductInLive - iaction");

			AssertService.assertTime((Date) liveMapUpdate.get("dEndTime"), (Date) liveProductMap.get("dEndTime"), 10,
					"ProductInLive - endtime");

			if (isLiveStart) {
				AssertService.assertTime(YMTDateUtil.getDate(), (Date) liveProductMap.get("dStartTime"), 120,
						"ProductInLive - startTime");
				Logger.verifyEquals(1, Integer.parseInt(liveProductMap.get("iSellStatus").toString()),
						"ProductInLive - iSellStatus");
			} else {
				AssertService.assertTime((Date) liveMapUpdate.get("dBeginTime"),
						(Date) liveProductMap.get("dStartTime"), 120, "ProductInLive - startTime");
				Logger.verifyEquals(0, Integer.parseInt(liveProductMap.get("iSellStatus").toString()),
						"ProductInLive - iSellStatus");

			}
		}
    	
    	
    	
    }
	
	
	
	//文描节点
	private static void setProductDescSaveModel(ModifySellerProductRequest modifySellerProductRequest,ProductSaveModel productSaveModel) throws JSONException{
		String productId = modifySellerProductRequest.getProductId();
		ProductDescSaveModel  productDescSaveModel = new ProductDescSaveModel();
		
		//ProductDescConfigWord
		IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
		List<Map<String,Object>> prodConfigWordList = ipm.selectDescConfigWordByProdIdAndSellerId(productId, modifySellerProductRequest.getSellerId());
		Logger.verifyEquals(1, prodConfigWordList.size(), "ProductDescConfigWord 文描记录应只有一条");
		Logger.verifyEquals(true, (Boolean)prodConfigWordList.get(0).get("Status"), "ProductDescConfigWord status 应为1");
		productDescSaveModel.setDescription(prodConfigWordList.get(0).get("WordContent").toString());
		
		
		//ProductDescConfigPicture
		List<String> prodConfigPicList = ipm.selectDescConfigPictureByProdIdAndSellerId(productId, modifySellerProductRequest.getSellerId()).stream()
													.map(x->x.get("PictureUrl").toString()).collect(Collectors.toList());
		
		GetProductAndCatalogInfoCall  infoCall=new GetProductAndCatalogInfoCall();
		JSONObject jsonObject=infoCall.callAndGetReturnData(modifySellerProductRequest.getSellerId(),modifySellerProductRequest.getProductId());
		JSONArray jsonArray=new JSONObject(new JSONObject(jsonObject.get("Data").toString()).get("ProductAndCatalogInfoOnSellerProduct").toString()).getJSONArray("DescPicList");
		//新增验证点
		if(prodConfigPicList.size()>0)
		{ 
			List<String> pics=modifySellerProductRequest.getProduct().getDescModel().getDescPicList();
			Logger.verifyEquals(pics.size(), prodConfigPicList.size(), "验证请求的文图个数与Ymt_ProductDescConfigPicture表的 一致");
			Logger.verifyEquals(pics.size(), jsonArray.length(), "验证请求的文图个数与详情接口返回的一致");

			for(int i=0;i<pics.size();i++)
			{
				Logger.verifyEquals(pics.get(i), prodConfigPicList.get(i), "验证Ymt_ProductDescConfigPicture表的 pictureUrl");
				Logger.verifyEquals(pics.get(i), jsonArray.get(i), "验证请求的图片与详情接口返回的 pictureUrl对比");
				Logger.verifyEquals(prodConfigPicList.get(i), jsonArray.get(i), "验证Ymt_ProductDescConfigPicture表的 pictureUrl与详情接口返回的 pictureUrl对比");
			}
		}
		productDescSaveModel.setDescPicList(prodConfigPicList);
		
		//Ymt_ProductDescMap
		List<Map<String,Object>> descMapList = ipm.selectDescMapPByProdId(productId);
		
		/*if (modifySellerProductRequest.getProduct().getDescModel().getBuyerNoticeId()>0||
				modifySellerProductRequest.getProduct().getDescModel().getDeclarationId()>0 || 
				modifySellerProductRequest.getProduct().getDescModel().getSellerServiceId()>0 ||
				modifySellerProductRequest.getProduct().getDescModel().getSizeTemplateId()>0)
		{*/
			Logger.verifyEquals(1, descMapList.size(), "Ymt_ProductDescMap 记录应只有一条");
			productDescSaveModel.setSizeTemplateId((Integer)descMapList.get(0).get("SizeTemplateId"));
			productDescSaveModel.setBuyerNoticeId((Integer)descMapList.get(0).get("BuyerNoticeId"));
			productDescSaveModel.setSellerServiceId((Integer)descMapList.get(0).get("SellerServiceId"));
			productDescSaveModel.setDeclarationId((Integer)descMapList.get(0).get("DeclarationId"));
		/*}else{
			Logger.verifyEquals(0, descMapList.size(), "Ymt_ProductDescMap 记录应没有");
		}*/
		
		
		productSaveModel.setDescModel(productDescSaveModel);
	}
	
	//视频节点
	private static void setproductVideoSaveModel(ModifySellerProductRequest modifySellerProductRequest,ProductSaveModel productSaveModel) {
		String productId = modifySellerProductRequest.getProductId();
		// Ymt_ProductVideo
		IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
		List<Map<String, Object>> prodVideoList = ipm.selectProdVideoByProdId(productId,modifySellerProductRequest.getSellerId());

		if (modifySellerProductRequest.getProduct().getVideoModel() != null) {
			Logger.verifyEquals(1, prodVideoList.size(), "Ymt_ProductVideo 记录应只有1条");
			ProductVideoSaveModel productVideoSaveModel = new ProductVideoSaveModel();
			productVideoSaveModel.setVideoUrl(prodVideoList.get(0).get("VideoUrl").toString());
			productVideoSaveModel.setLength((Integer) prodVideoList.get(0).get("VideoLength"));
			productVideoSaveModel.setPic(prodVideoList.get(0).get("VideoPic").toString());
			productVideoSaveModel.setWidth((Integer) prodVideoList.get(0).get("VideoWidth"));
			productVideoSaveModel.setHeight((Integer) prodVideoList.get(0).get("VideoHeight"));
			productVideoSaveModel.setSize((Integer) prodVideoList.get(0).get("VideoSize"));

			productSaveModel.setVideoModel(productVideoSaveModel);
		} else {
			Logger.verifyEquals(0, prodVideoList.size(), "Ymt_ProductVideo 记录应没有");
		}
	}
	
	
	//属性
	private static void setProductPropertySaveModel(ModifySellerProductRequest modifySellerProductRequest,ProductSaveModel productSaveModel){
		String productId = modifySellerProductRequest.getProductId();
		// Ymt_ProductProperty
		IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
		List<Map<String, Object>> prodPropertyList = ipm.selectPropertyByProdId(productId);
		List<ProductPropertySaveModel> propertyList = new ArrayList<>();

		if (modifySellerProductRequest.getProduct().getPropertyList() == null
				|| modifySellerProductRequest.getProduct().getPropertyList().size() == 0) {
			Logger.verifyEquals(0, prodPropertyList.size(), "Ymt_ProductProperty 记录应没有");
		} else {
			prodPropertyList.forEach(ele -> {
				ProductPropertySaveModel productPropertySaveModel = new ProductPropertySaveModel();
				productPropertySaveModel.setPropertyId((Integer) ele.get("PropertyId"));
				productPropertySaveModel.setPropertyValues(Arrays.asList(ele.get("Value").toString().split(",")));
				propertyList.add(productPropertySaveModel);
			});

		}
		productSaveModel.setPropertyList(propertyList);
				
	}
	
	//运费模板
	private static void setProductFreightSaveModel(ModifySellerProductRequest modifySellerProductRequest,ProductSaveModel productSaveModel){
		String productId = modifySellerProductRequest.getProductId();
		// Ymt_ProductDeliveryTemplate
		IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
		List<Integer> tplIdList = modifySellerProductRequest.getProduct().getFreightTemplateList().stream()
				.map(x -> x.getTemplateId()).collect(Collectors.toList());
		List<ProductFreightSaveModel> delTplModelList = new ArrayList<>();

		for (Integer tplId : tplIdList) {
			List<Map<String, Object>> prodTplList = ipm.selectDeliveryTplByProductIdAndTplId(productId, tplId);
			Logger.verifyEquals(1, prodTplList.size(), "Ymt_ProductDeliveryTemplate 记录应只有一条");
			ProductFreightSaveModel productFreightSaveModel = new ProductFreightSaveModel();
			productFreightSaveModel.setTemplateId(tplId);
			productFreightSaveModel.setCatalogStatus((Integer) prodTplList.get(0).get("CatalogStatus"));
			delTplModelList.add(productFreightSaveModel);
		}

		productSaveModel.setFreightTemplateList(delTplModelList);
	}
	
	
	private static String getProductGrade(ProductSaveModel product) {
        if (product == null || product.getDescModel() == null
                || product.getDescModel().getDescription()== null) {
            return "C";
        }

        if (product.getDescModel().getDescPicList().isEmpty()) {
            return "C";
        }

        return product.getProductPicList().isEmpty() || product.getProductPicList().size() < 3
                ? "C" : "B";
    }
	
	//check mongo
    private static void checkProductMgo(ModifySellerProductRequest modifySellerProductRequest) throws Exception{
    	String productId = modifySellerProductRequest.getProductId();
    	if (YmtMongoDriverFactroy.IsNewJavaMongoOpen){
			List<Integer> liveIds = modifySellerProductRequest.getProduct().getLiveIdList();
			if (liveIds == null || liveIds.size()==0){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}else{
				for(Integer lid :liveIds ){
					NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(lid, productId);
				}
			}
		}
    }
}
